<template>
    <div>
        <div class="buttonTop">
            <el-button type="primary" round @click="add">添加</el-button>
        </div>
        <el-dialog
            :title="title"
            :visible.sync="dialogVisible"
            width="40%"
            :before-close="handleClose"
        >
            <el-form ref="form" :model="form" label-width="80px">
                <el-row>
                    <el-col :span="2">&nbsp;</el-col>
                    <el-col :span="18">
                        <el-form-item label="上级分类">
                            <template>
                                <el-select v-model="form.pid" placeholder="请选择" @change="change">
                                    <el-option
                                        v-for="item in options"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    ></el-option>
                                </el-select>
                            </template>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">&nbsp;</el-col>
                </el-row>
                <!-- 商品名称 -->
                <el-row>
                    <el-col :span="2">&nbsp;</el-col>
                    <el-col :span="18">
                        <el-form-item label="分类名称">
                            <el-input v-model="form.categoryName" placeholder="请输入名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">&nbsp;</el-col>
                </el-row>

                <!-- 提交按钮 -->
                <el-row>
                    <el-col :span="2">&nbsp;</el-col>
                    <el-col :span="16">
                        <el-button type="primary" @click="submit">提交</el-button>
                    </el-col>
                    <el-col :span="6">&nbsp;</el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
            </span>
        </el-dialog>

        <el-table
            :stripe="isStripe"
            ref="multipleTable"
            :data="categoryData"
            tooltip-effect="dark"
            style="width: 100%"
            @selection-change="handleSelectionChange"
        >
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column fixed="left" prop="id" label="编号" width="220"></el-table-column>
            <el-table-column prop="categoryName" label="名称" width="420"></el-table-column>

            <el-table-column label="操作" width="320">
                <template slot-scope="scope">
                    <el-button size="mini" @click="edit(scope.row)">编辑</el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>
<script>
import { update, add, selectAll, del } from "@/api/goods/category.js";

export default {
    created() {
        selectAll().then(response => {
            console.log(response.data.data);
            this.categoryData = response.data.data;
        });
    },
    data() {
        return {
            dialogVisible: false,
            form: {
                id: "",
                categoryName: "",
                pid: "",

            },
            title: "",
            isStripe: true,
            categoryData: [],
            options: [],
            isChange: true,
        };
    },
    methods: {
        //判断是否选择了 
        change(event){
            console.log("change: " + event);
            this.isChange = false;
            
        },
        handleClose() {
            this.dialogVisible = false;
        },
        handleDelete(row) {
            this.$confirm("确认删除该类及其子类?").then(() => {
                //删除子类
                this.categoryData.forEach((element, index) => {
                    if (element.pid == row.id) {
                        //后端 前端都删 省去接口调用 后端前移的思想 减少服务器消耗
                        // console.log(element.id);
                        del(element.id);
                        this.categoryData.splice(index, 1);
                    }
                });
                //删除自己
                this.categoryData.forEach((element, index) => {
                    if (element.id == row.id) {
                        del(row.id);
                        this.categoryData.splice(index, 1);
                    }
                });
            });
        },
        edit(row) {
            this.title = "编辑分类";
            this.form.categoryName = row.categoryName;
            console.log(this.form.categoryName);
            this.form.id = row.id;
            let option = {
                label: "",
                value: ""
            };
            this.categoryData.forEach(element => {
                //找出父类
                if (element.id == row.pid) {
                    option.label = element.categoryName;
                    option.value = element.id;
                    this.options.push(option);
                }
            });
            this.dialogVisible = true;
        },
        add() {
            this.options = [];
            this.title = "添加分类";
            this.categoryData.forEach(element => {
                //选出一级分类
                let option = {
                    label: "",
                    value: ""
                };
                if (element.pid == -1) {
                    option.label = element.categoryName;
                    option.value = element.id;
                    this.options.push(option);
                }
            });

            this.dialogVisible = true;
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        submit() {
            if (this.title == "编辑分类") {
                update(this.form);
                this.dialogVisible = false;
            } else {
                //增加分类
                if(this.isChange){
                    this.form.pid = -1
                    console.log("fjiefie")
                }
                add(this.form).then(response => {
                    alert(response.data.msg);
                });
            }
            this.dialogVisible = false;
            //提交之后重新请求页面
            selectAll().then(response => {
                console.log(response.data.data);
                this.categoryData = response.data.data;
            });
        }
    }
};
</script>
<style scoped>
.buttonTop {
    float: left;
    margin-left: 60px;
}
</style>